<?php

namespace App\depository;

use App\Models\MarkModel;
use App\Models\UserHasTeamsModel;
use Illuminate\Support\Facades\DB;

class MarkDep
{
    public $model;

    public function __construct()
    {
        $this->model = new MarkModel();
        $this->teams = new UserHasTeamsModel();
    }

    public function find($param)
    {
        return $this->model->where($param)->first();
    }

    public function jurisdiction($user_id, $teams)
    {
        return $this->model->select(DB::raw('count(*) as sum, steamid'))->where(function ($query) use ($user_id, $teams) {
            $query->where('jurisdiction', 3);
            $query->orWhere(function ($subQuery) use ($teams) {
                $subQuery->where('gh_id', $teams)
                    ->where('jurisdiction', 2);
            });
            $query->orwhere('user_id', $user_id);
        })->groupBy('steamid')->get();
    }

    public function list($param, $start = true)
    {
        $res = MarkModel::select([
            'manage_mark.id',
            'manage_mark.user_id',
            'manage_mark.steamid',
            'manage_mark.content',
            'manage_mark.jurisdiction',
            'manage_mark.gh_id',
            'u.name as name',
            't.name as gh_name',
            'manage_mark.created_at',
            'manage_mark.updated_at'
        ])
            ->leftJoin('users as u', 'manage_mark.user_id', '=', 'u.id')
            ->leftJoin('teams as t', 'manage_mark.gh_id', '=', 't.id');
        if ($start) {
            $res = $res->where('manage_mark.steamid', $param['steamid'])
                ->where(function ($query) use ($param) {
                    $query->where('manage_mark.jurisdiction', 3);
                    $query->orWhere(function ($subQuery) use ($param) {
                        $subQuery->where('manage_mark.gh_id', $param['gh_id'])
                            ->where('manage_mark.jurisdiction', 2);
                    });
                    $query->orwhere('manage_mark.user_id', $param['user_id']);
                });
        } else {
            $res = $res->when(!empty($param['steamid']), function ($query) use ($param) {
                return $query->where("steamid", 'like', "%{$param['steamid']}%");
            })->when(!empty($param['gh_id']), function ($query) use ($param) {
                return $query->where("gh_id", $param['gh_id']);
            })->when(!empty($param['name']), function ($query) use ($param) {
                return $query->where("user_id", $param['name']);
            })->when(!empty($param['date']), function ($query) use ($param) {
                return $query->where("manage_mark.created_at", '>', $param['date'][0])->where("manage_mark.created_at", '<', $param['date'][1]);
            })->when(!empty($param['content']), function ($query) use ($param) {
                return $query->where("content", 'like', "%{$param['content']}%");
            });
        }
        // else {
        //     $teams = $this->teams->where('user_id', $param['user_id'])->select('team_id')
        //         ->get();
        //     $res = $res->when(!empty($param['gh_name']), function ($query) use ($param) {
        //         return $query->where("t.name", $param['gh_name']);
        //     })->where(function ($query) use ($param, $teams) {
        //         $query->where('manage_mark.jurisdiction', 3);
        //         $query->orWhere(function ($subQuery) use ($param, $teams) {
        //             $subQuery->where('manage_mark.gh_id', 'in', $teams)
        //                 ->where('manage_mark.jurisdiction', 2);
        //         });
        //         $query->orwhere('user_id', $param['user_id']);
        //     });
        // }
        $res = $res->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);
        return $res;
    }


    public function add($data)
    {
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function upd($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->delete();
        return $res;
    }
}
